<?php

namespace App\HttpController\XAppSystem\Account\Admin;


use App\Common\OA\OA;
use App\Common\SysEnum;
use App\HttpController\ControllerAdmin;
use App\Model\t_s_account;
use XWX\Common\H;
use XWX\Common\Validate\Validate;

class AccountList extends ControllerAdmin
{
    public function index()
    {
        $compKw = $this->reqParam('compKw');


        $query = $this->db()
            ->where('acc_x_id', $this->funcGetX())
            ->where('acc_role', 'super_admin', '<>')
            ->orderBy('acc_create_date');

        if (H::funcStrHasAnyText($compKw))
        {
            $query->where('acc_name', "%{$compKw}%", 'LIKE');
        }

        $query_count = clone $query;

        $list = $query->get(t_s_account::table(), [$this->funcGetPageIndexXPage(), $this->funcGetPageSize()]);
        $count = $query_count->count(t_s_account::table());


        //二次加工
        $list_ = [];
        foreach ($list as $v)
        {
            $data = $v;

            $data['id'] = $v['acc_id'];
            $data['acc_role_str'] = SysEnum::funcGet("acc_role.{$v['acc_role']}");

            $list_[] = $data;
        }


        //获取超管UID
        $super_admin = $this->db()
            ->where('acc_x_id', $this->funcGetX())
            ->where('acc_role', 'super_admin')
            ->getOne(t_s_account::table());


        $this->commonR()->setData("list", $list_);
        $this->commonR()->setData("count", $count);
        $this->commonR()->setData("super_uid", H::funcArrayGet($super_admin, 'acc_uid'));


        $this->funcWriteCommonR();
    }


    public function save()
    {
        $v = new Validate();
        $v->addColumn('acc_uid')
            ->required()
            ->func(function ($val)//验证账号是否存在
            {
                $query = $this->db()
                    ->where('acc_x_id', $this->funcGetX())
                    ->where('acc_uid', $val);
                if ($this->funcIsAdminEdit())
                {
                    $query->where('acc_uid', $this->reqParam('acc_uid'), '<>');
                }

                $count = $query->count(t_s_account::table());
                if ($count > 0)
                {
                    return $this->funcGetR(-1071, '帐号已存在');
                }

                return $this->funcGetR(0);
            });
        $v->addColumn('acc_name')->required();
        $v->addColumn('acc_role')->required();

        if (!$v->validate($this->request()->getRequestParam(), true))
        {
            return $this->funcWriteValidate($v->getErrors());
        }


        $this->funcSave(t_s_account::class, null, false);


        $this->funcWriteCommonR();
    }

    public function funcSaveBefore($entity)
    {
        if ($this->funcIsAdminAdd())
        {
            //默认密码
            $pwd = '123456';

            $entity->acc_pwd = OA::funcGetEncPwd($this->funcGetX(), $pwd);
            $entity->acc_role = 'admin';
        }
    }


    //删除
    public function del()
    {
        $ids = $this->reqParam('ids');

        $r = $this->funcDel(t_s_account::class, $ids);


        $this->funcWriteErrR($r);
    }

    //重置密码
    public function resetPwd()
    {
        $ids_str = $this->reqParam('ids');
        $ids = explode('.', $ids_str);


        $enc_pwd = OA::funcGetEncPwd($this->funcGetX(), '123456');

        $res = $this->db()->whereIn('acc_id', $ids)
            ->update(t_s_account::table(), [
                'acc_pwd' => $enc_pwd,
            ], 1);
        if ($res == false)
        {
            $errmsg = $this->db()->getLastError();
            $this->commonR()->errcode = -123456;
            $this->commonR()->errmsg = '删除失败,' . $errmsg;
        }


        $this->funcWriteCommonR();
    }
}